checkSkuHttp
- 🇬🇧 English
- 🇮🇹 Italiano
Function Name: checkSKUHttp
Author: Domenico Cerone Creation Date: 25/09/2025
Last Reviewer: Domenico Cerone
Trigger: HTTPS (onRequest)
Purpose: Verifies the availability of a list of codes (SKU, EAN or UPC) through the Firestore database and Safilo API, returning the status of each product.
Detailed Functionality​
This Firebase Function performs the following operations in sequence:
1. Input Validation​
- Accepts arrays of objects containing
skuCode,eanCodeand/orupcCode - Also supports the
{ skuList: [...] }format - Validates that at least one non-empty array is provided
2. Firestore Database Check​
- For each element, searches in the
Variantscollection using:skuModelfor comparison withskuCodeeanCodefor comparison witheanCodeupcCodefor comparison withupcCode
- Retrieves product status if found
3. Safilo API Check with Fallback Logic​
- First call: Uses all available parameters together
- Sequential fallback: If the first fails, tries:
- Only
skuCode(if available) - Only
eanCode(if available) - Only
upcCode(if available)
- Only
- Stops at the first positive result (status 200 with data)
- Properly handles 404 errors (product not found) vs 500+ errors (API error)
4. Extended Search with API Data​
- If the API returns data but Firestore finds no results
- Performs a second search in Firestore using codes from the API
- Searches with
sku,eanCodeandupcreturned by the API
5. Status Determination (result)​
- "Available": Product exists in Firestore with "Pubblicato" status
- "In progress": Product exists in Firestore but not published
- "Orderable": Product not in Firestore but available via Safilo API
- "Error": Product not found in either Firestore or via API
6. API Status Determination​
- "success": API returned status 200 with data
- "Failed - Not Found": API returned 404 (product not found)
- "Failed - API": Technical API error (status 500+, timeout, network errors)
7. Batch Management and Performance​
- Batch size: 75 elements per batch
- Processing: Sequential batches, parallel elements within batch
- Delay: 100ms between API calls to avoid overload
- Timeout: 30 seconds per single API call, 540 seconds (9 minutes) total
- Memory: 1GiB allocated to handle large volumes
8. Progress Streaming​
- Sends real-time progress updates
- Includes
progress(percentage),totalItems,processedItems - Uses HTTP chunked streaming for incremental updates
- Frontend can display progress bars based on this data
9. Technical Configuration​
- API Server: commportal-api.safilo.com/damAPI (updated API)
- Region: europe-central2
- CORS: Enabled for frontend calls
- Axios: Configured with 30s timeout and keep-alive
- Error Handling: Specific handling for ECONNRESET and network errors
10. Usage Examples​
- Single verification:
[{"skuCode": "123456"}] - Multiple verification: Array with mix of SKU, EAN, UPC
- Large batches: Up to 250+ elements handled automatically
- Progress monitoring: Streaming response for reactive UI
11. Response Logic​
Each processed element returns:
- Original + enriched codes: Input data + any data from API
- result: Final product status
- isSelected:
trueonly ifresultis "Available" - status: API call status
The function optimises performance for large data volumes whilst maintaining responsiveness through streaming and batch processing, ensuring robustness through multiple fallbacks and comprehensive error handling.
Input (Payload):​
For the HTTP function:
[
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046"
},
{
"skuCode": "1093728RU5219"
},
{
"eanCode": "0716736419046"
},
{
"upcCode": "xxxxx"
}
]
Or in the alternative format:
{
"skuList": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046"
}
]
}
Output (Success):​
{
"progress": 100,
"totalItems": 4,
"processedItems": 4,
"results": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046",
"result": "Available",
"isSelected": true,
"status": "success"
},
{
"skuCode": "1093728RU5219",
"eanCode": "",
"upcCode": "",
"result": "In progress",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "0716736419046",
"upcCode": "",
"result": "Orderable",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "",
"upcCode": "xxxxx",
"result": "Error",
"isSelected": false,
"status": "Failed - Not Found"
}
]
}
This output is returned in the HTTP response (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp
Test with Emulator:
- Start the Firebase emulator:
firebase emulators:start --only functions - Ensure you're using Node.js version 20:
nvm use 20 - Test with curl:
curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp" \
-H "Content-Type: application/json" \
-d '[{"skuCode": "3102687C55110", "eanCode": "0716736419046"}]'
Postman Testing:
- Method: POST
- URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp
- Headers:
- Key:
Content-Type - Value:
application/json
- Key:
- Body: raw JSON (see examples above)
Deploy Command:​
firebase deploy --only functions:checkSKUHttp
Production URL:​
Live Function: https://europe-central2-arshades-7e18a.cloudfunctions.net/checkSKUHttp
Function Name: checkSKUHttp
Autore: Domenico Cerone Data di creazione: 25/09/2025
Last Reviewer: Domenico Cerone
Trigger: HTTPS (onRequest)
Purpose: Verifica la disponibilità di una lista di codici (SKU, EAN o UPC) attraverso il database Firestore e l'API Safilo, restituendo lo stato di ogni prodotto.
Funzionamento Dettagliato​
Questa Firebase Function esegue le seguenti operazioni in sequenza:
1. Validazione Input​
- Accetta array di oggetti contenenti
skuCode,eanCodee/oupcCode - Supporta anche il formato
{ skuList: [...] } - Valida che almeno un array non vuoto sia fornito
2. Controllo Database Firestore​
- Per ogni elemento, cerca nella collezione
Variantsutilizzando:skuModelper confronto conskuCodeeanCodeper confronto coneanCodeupcCodeper confronto conupcCode
- Recupera lo stato del prodotto se trovato
3. Controllo API Safilo con Logica di Fallback​
- Prima chiamata: Utilizza tutti i parametri disponibili insieme
- Fallback sequenziale: Se la prima fallisce, prova:
- Solo
skuCode(se disponibile) - Solo
eanCode(se disponibile) - Solo
upcCode(se disponibile)
- Solo
- Si ferma al primo risultato positivo (status 200 con dati)
- Gestisce correttamente errori 404 (prodotto non trovato) vs errori 500+ (errore API)
4. Ricerca Estesa con Dati API​
- Se l'API restituisce dati ma Firestore non trova risultati
- Effettua una seconda ricerca in Firestore usando i codici dall'API
- Cerca con
sku,eanCodeeupcrestituiti dall'API
5. Determinazione dello Stato (result)​
- "Available": Prodotto esiste in Firestore con stato "Pubblicato"
- "In progress": Prodotto esiste in Firestore ma non pubblicato
- "Orderable": Prodotto non in Firestore ma disponibile via API Safilo
- "Error": Prodotto non trovato né in Firestore né via API
6. Determinazione Status API​
- "success": API ha restituito status 200 con dati
- "Failed - Not Found": API ha restituito 404 (prodotto non trovato)
- "Failed - API": Errore tecnico API (status 500+, timeout, errori di rete)
7. Gestione Batch e Performance​
- Dimensione batch: 75 elementi per batch
- Elaborazione: Batch sequenziali, elementi in parallelo all'interno del batch
- Delay: 100ms tra chiamate API per evitare sovraccarico
- Timeout: 30 secondi per singola chiamata API, 540 secondi (9 minuti) totali
- Memoria: 1GiB allocata per gestire grandi volumi
8. Streaming di Progresso​
- Invia aggiornamenti di progresso in tempo reale
- Include
progress(percentuale),totalItems,processedItems - Utilizza streaming HTTP chunked per aggiornamenti incrementali
- Il frontend può mostrare barre di progresso basate su questi dati
9. Configurazione Tecnica​
- Server API: commportal-api.safilo.com/damAPI (API aggiornata)
- Regione: europe-central2
- CORS: Abilitato per chiamate da frontend
- Axios: Configurato con timeout 30s e keep-alive
- Error Handling: Gestione specifica per ECONNRESET e errori di rete
10. Esempi di Utilizzo​
- Verifica singola:
[{"skuCode": "123456"}] - Verifica multipla: Array con mix di SKU, EAN, UPC
- Batch grandi: Fino a 250+ elementi gestiti automaticamente
- Monitoraggio progresso: Risposta streaming per UI reattive
11. Logica di Risposta​
Ogni elemento processato restituisce:
- Codici originali + arricchiti: Dati input + eventuali dati dall'API
- result: Stato finale del prodotto
- isSelected:
truesolo seresultè "Available" - status: Stato della chiamata API
La funzione ottimizza le performance per grandi volumi di dati mantenendo la responsività attraverso streaming e batch processing, garantendo robustezza tramite fallback multipli e gestione completa degli errori.
Input (Payload):​
Per la funzione HTTP:
[
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046"
},
{
"skuCode": "1093728RU5219"
},
{
"eanCode": "0716736419046"
},
{
"upcCode": "xxxxx"
}
]
Oppure nel formato alternativo:
{
"skuList": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046"
}
]
}
Output (Success):​
{
"progress": 100,
"totalItems": 4,
"processedItems": 4,
"results": [
{
"skuCode": "3102687C55110",
"eanCode": "0716736419046",
"upcCode": "716736419046",
"result": "Available",
"isSelected": true,
"status": "success"
},
{
"skuCode": "1093728RU5219",
"eanCode": "",
"upcCode": "",
"result": "In progress",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "0716736419046",
"upcCode": "",
"result": "Orderable",
"isSelected": false,
"status": "success"
},
{
"skuCode": "",
"eanCode": "",
"upcCode": "xxxxx",
"result": "Error",
"isSelected": false,
"status": "Failed - Not Found"
}
]
}
Questo output viene restituito nella risposta HTTP (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp
Test with Emulator:
- Avviare l'emulatore Firebase:
firebase emulators:start --only functions - Assicurarsi di usare Node.js versione 20:
nvm use 20 - Testare con curl:
curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp" \
-H "Content-Type: application/json" \
-d '[{"skuCode": "3102687C55110", "eanCode": "0716736419046"}]'
Postman Testing:
- Metodo: POST
- URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/checkSKUHttp
- Headers:
- Key:
Content-Type - Value:
application/json
- Key:
- Body: raw JSON (vedi esempi sopra)
Deploy Command:​
firebase deploy --only functions:checkSKUHttp
URL di Produzione:​
Funzione Live: https://europe-central2-arshades-7e18a.cloudfunctions.net/checkSKUHttp